import time
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from app_wechat_po.utils.log_utils import logger


##基本的方法：driver, find, click, swipe
class BasePage:
    implicitly_wait_time = 20

    # driver 不希望被暴漏出去
    def __init__(self, driver:WebDriver=None):
        self.driver = driver

    # 查找元素
    def find(self, by, locator):
        return self.driver.find_element(by, locator)

    # 查找元素
    def finds(self, by, locator):
        return self.driver.find_elements(by, locator)

    # 查找元素点击
    def find_and_click(self, by, locator):
        self.find(by, locator).click()

    # 查找元素输入内容
    def fing_and_send(self, by, locator, text):
        self.find(by, locator).send_keys(text)

    # 获取文本信息
    def find_and_gettext(self, by, locator):
        return self.find(by, locator).text

    # toast 封装
    def get_toast_text(self):
        toast_text = self.find(MobileBy.XPATH, "//*[@class='android.widget.Toast']").text
        return toast_text

    # 截图
    def screenshot(self, name):
        self.driver.save_screenshot(name)

    # 时间格式
    def get_time(self):
        t = time.localtime(time.time())
        cur_time = time.strftime("%Y-%m-%d_%H_%M_%S, t")
        print(f"当前时间为：{cur_time}")
        return cur_time

    # 设置隐式等待时长
    def set_implicitly(self, second):
        self.driver.implicitly_wait(second)

    # 获取屏幕尺寸方法 向上滑动
    def swipe_up(self):
        # 'width', 'height'
        size = self.driver.get_window_size()
        width = size.get("width")
        height = size.get("height")
        logger.info(f"当前屏幕的宽：{width}, 高：{height}")
        start_x = width / 2
        start_y = height * 0.8
        end_x = start_x
        end_y = height * 0.3
        duration = 2000
        logger.info(f"开始滑动:{start_x},{start_y} to {end_x},{end_y}")
        self.driver.swipe(start_x, start_y, end_x, end_y, duration)

    # 滑动方法
    def swipe_find(self, text, num=3):
        # 自定义滑动查找，
        self.set_implicitly(1)
        for i in range(num):
            try:
                element = self.find(MobileBy.XPATH, f"//*[@text='{text}']")
                self.set_implicitly(self.implicitly_wait_time)
                return element
            except:
                logger.info("未找到元素，开始滑动")
                self.swipe_up()
            if i == num - 1:
                self.set_implicitly(self.implicitly_wait_time)
                raise NoSuchElementException(f"找了{num}次 ，未找到元素{text}")

    # 返回
    def back(self, num=1):
        for i in range(num):
            self.driver.back()

    # 等待元素出现
    def wait_show(self,text):
        return WebDriverWait(self.driver, 5).until(
            expected_conditions.text_to_be_present_in_element((
                MobileBy.XPATH, f"//*[@text='{text}']"), text))

    # 等待元素消失
    def wait_disappear(self,locator):
        WebDriverWait(self.driver, 5).until_not(lambda x: x.find_element(*locator))
